﻿using System;
using System.Collections.Generic;
using ExFlash.Actions;

namespace ExFlash.Records
{
    public class ButtonCondAction : ISwfSerializable
    {
        private readonly int _swfVersion;

        public ushort CondActionSize
        {
            get;
            protected set;
        }

        public Boolean CondIdleToOverDown
        {
            get;
            protected set;
        }

        public Boolean CondOutDownToIdle
        {
            get;
            protected set;
        }

        public Boolean CondOutDownToOverDown
        {
            get;
            protected set;
        }

        public Boolean CondOverDownToOutDown
        {
            get;
            protected set;
        }

        public Boolean CondOverDownToOverUp
        {
            get;
            protected set;
        }

        public Boolean CondOverUpToOverDown
        {
            get;
            protected set;
        }

        public Boolean CondOverUpToIdle
        {
            get;
            protected set;
        }

        public Boolean CondIdleToOverUp
        {
            get;
            protected set;
        }

        public Byte CondKeyPress
        {
            get;
            protected set;
        }

        public Boolean CondOverDownToIdle
        {
            get;
            protected set;
        }

        public List<ActionRecord> Actions
        {
            get;
            protected set;
        }

        public ButtonCondAction(int version)
        {
            _swfVersion = version;
        }

        #region ISwfSerializable Members

        public void SwfDeserialize(BinaryBitReader reader)
        {
            CondActionSize = reader.ReadUInt16();

            CondIdleToOverDown = reader.ReadBoolean();
            CondOutDownToIdle = reader.ReadBoolean();
            CondOutDownToOverDown = reader.ReadBoolean();
            CondOverDownToOutDown = reader.ReadBoolean();
            CondOverDownToOverUp = reader.ReadBoolean();
            CondOverUpToOverDown = reader.ReadBoolean();
            CondOverUpToIdle = reader.ReadBoolean();
            CondIdleToOverUp = reader.ReadBoolean();

            CondKeyPress = (Byte) reader.ReadUInt32(7);
            CondOverDownToIdle = reader.ReadBoolean();

            reader.AlignBits();

            Actions = ActionAssistant.GetActions(reader, _swfVersion);
        }

        public void SwfSerialize(byte[] arr, ref int offset)
        {
            throw new NotImplementedException();
        }

        public static List<ButtonCondAction> GetButtonCondActionList(BinaryBitReader reader, int version)
        {
            List<ButtonCondAction> list = new List<ButtonCondAction>();
            ButtonCondAction rec;

            while (reader.PeekUInt16() != 0)
            {
                rec = new ButtonCondAction(version);
                rec.SwfDeserialize(reader);
                list.Add(rec);
            }

            rec = new ButtonCondAction(version);
            rec.SwfDeserialize(reader);
            list.Add(rec);

            return list;
        }

        #endregion
    }
}
